package cn.chinaunicom.sdsi.frm.workflow.sample.controller;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chinaunicom.sdsi.frm.workflow.sample.service.MultiInstanceService;
import net.sf.json.JSONObject;

/**
 * 多实例任务，投票流程示例控制器
 * 
 * @author liyh
 *
 */
@Controller("multiInstanceController")
@RequestMapping(value = "/multiInstance")
public class MultiInstanceController {

	private final Log log = LogFactory.getLog(MultiInstanceController.class);

	/**
	 * 工作流任务相关的业务逻辑组件
	 */
	private MultiInstanceService multiInstanceService;

	/**
	 * 跳转至公共业务发起页面
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/multiInstanceDetail")
	public String multiInstanceDetail() {
		log.info("跳转到业务页面模拟流程发起！");
		return "frm/workflow/sample/common-detail";
	}

	/**
	 * 跳转至流程办理的业务页面
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/multiInstanceTask")
	public String multiInstanceTask() {
		log.info("跳转到业务页面模拟流程处理！");
		return "frm/workflow/sample/multiInstance-task";
	}

	/**
	 * 跳转到会签环节前任务处理页面
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/multiInstanceTaskPreVote")
	public String multiInstanceTaskPreVote() {
		log.info("跳转到会签环节前任务处理页面！");
		return "frm/workflow/sample/multiInstance-task-pre";
	}

	/**
	 * 跳转到会签环节处理页面
	 * 
	 * @return
	 */
	@RequestMapping("/multiInstanceTaskVote")
	public String multiInstanceTaskVote() {
		log.info("跳转到会签环节任务处理页面！");
		return "frm/workflow/sample/multiInstance-task-vote";
	}

	@RequestMapping("/startProcess")
	@ResponseBody
	public JSONObject startProcess(String processDefinitionKey, String assignee, String taskDefinitionKey) {
		/*
		 * 在控制层处理完参数后即可调用业务模块的服务层，并注意控制事务
		 */
		multiInstanceService.startProcess(processDefinitionKey, assignee, taskDefinitionKey);

		return new JSONObject().element("sign", "success");
	}

	/**
	 * 流程推进参数说明
	 * 
	 * @param taskDefinitionKey
	 *            目标环节定义编码
	 * @param assignee
	 *            目标环节受理人
	 * @param processInstanceId
	 *            流程实例ID
	 * @param taskId
	 *            任务ID
	 * @param comment
	 *            处理意见
	 * @param handleType
	 *            处理类型，1通过，2回退
	 * @param withdraw
	 *            此次操作是否可逆，即推进后是否可退回
	 * @return 业务数据返回
	 */
	@RequestMapping("/pushProcess")
	@ResponseBody
	public JSONObject pushProcess(String taskDefinitionKey, String assignee, String processInstanceId, String taskId,
			String comment, int handleType, int withdraw) {
		/*
		 * 在控制层处理完参数后即可调用业务模块的服务层，并注意控制事务，此处仅示例，具体所需参数业务模块自行决定
		 */
		multiInstanceService.pushProcess(processInstanceId, taskId, taskDefinitionKey, assignee, handleType, withdraw,
				comment);

		return new JSONObject().element("sign", "success");
	}

	/**
	 * 会签环节前选人环节推荐方法
	 * 
	 * @param assignees
	 *            目标环节受理人多人拼接串
	 * @param processInstanceId
	 *            流程实例ID
	 * @param taskId
	 *            任务ID
	 * @param comment
	 *            处理意见
	 * @param handleType
	 *            处理类型，1通过，2回退
	 * @return 业务数据返回
	 */
	@RequestMapping("/pushProcessBeforeVote")
	@ResponseBody
	public JSONObject pushProcess(String assignees, String processInstanceId, String taskId, String comment,
			int handleType) {

		// 处理受理人组数据
		String[] assigneeArray = assignees.split(",");
		List<String> list = Arrays.asList(assigneeArray);

		multiInstanceService.pushProcess(processInstanceId, taskId, list, handleType, comment);

		return new JSONObject().element("sign", "success");
	}

	/**
	 * 
	 * 
	 * @param processInstanceId
	 *            流程实例ID
	 * @param taskId
	 *            任务Id
	 * @param comment
	 *            用户意见
	 * @param handleType
	 *            处理类型，1通过，2拒绝
	 * @return 业务数据返回
	 */
	@RequestMapping("/pushProcessVote")
	@ResponseBody
	public JSONObject pushProcess(String processInstanceId, String taskId, String comment, int handleType) {

		// 会签环节处理方式，利用引擎原生方式仅做环节完成
		multiInstanceService.pushProcess(processInstanceId, taskId, handleType, comment);

		return new JSONObject().element("sign", "success");
	}

	@RequestMapping("/breakProcess")
	@ResponseBody
	public JSONObject breakProcess(String processInstanceId, String taskId, String comment) {
		multiInstanceService.breakProcess(processInstanceId, taskId, comment);

		return new JSONObject().element("sign", "success");
	}

	@RequestMapping("/withdrawProcess")
	@ResponseBody
	public JSONObject withdrawProcess(String processInstanceId, String taskId) {
		multiInstanceService.withdrawProcess(processInstanceId, taskId);

		return new JSONObject().element("sign", "success");
	}

	@Autowired
	public void setMultiInstanceService(MultiInstanceService multiInstanceService) {
		this.multiInstanceService = multiInstanceService;
	}

}
